package com.ebo.synframework.synroom.executor;

import org.jboss.netty.channel.ChannelHandlerContext;


public abstract class SynRoomTask implements Runnable {
    
    protected final boolean isImportantTask;
    
    protected final SynRoom synRoom;

    protected ChannelHandlerContext channelCtx;
    
    public SynRoomTask(SynRoom synRoom) {
        this.synRoom = synRoom;
        this.isImportantTask = false;
    }

    public SynRoomTask(SynRoom synRoom, boolean isImportantTask) {
        this.synRoom = synRoom;
        this.isImportantTask = isImportantTask;
    }
    
    public SynRoomTask(SynRoom synRoom, ChannelHandlerContext channelCtx) {
        this.synRoom = synRoom;
        this.channelCtx = channelCtx;
        this.isImportantTask = false;
    }

    public SynRoomTask(SynRoom synRoom, ChannelHandlerContext channelCtx, boolean isImportantTask) {
        this.synRoom = synRoom;
        this.channelCtx = channelCtx;
        this.isImportantTask = isImportantTask;
    }

    public final boolean isSynRoomTaskProcessing() {
        return synRoom.isProcessing();
    }
    
    public final boolean isImportantTask() {
        return this.isImportantTask;
    }

    public final void setSynRoomTaskProcessing(boolean isProcessing) {
        synRoom.setProcessing(isProcessing);
    }

    @Override
    public void run() {
        try {
            processTask();
        } finally {
            synRoom.setProcessing(false);
        }
    }

    public abstract void processTask();

    public int getPoolIndex() {
        return this.synRoom.getPoolIndex();
    }

    public boolean casPoolIndex(int poolIndex) {
        return this.synRoom.casPoolIndex(poolIndex);
    }

}
